package com.aischool.webapp.teacher;

import com.aischool.common.model.AiStudent;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiClassService;
import com.aischool.service.AiParentService;
import com.aischool.service.AiStudentService;
import com.aischool.service.AiTeacherService;
import com.aischool.service.FileUploadService;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.jfinal.aop.Inject;

@RouteMapping(url = "/front/teacher/students")
public class AiStudentsController extends BaseTeacherController {


	@Inject
	AiTeacherService aiTeacherService;
	@Inject
	AiStudentService aiStudentService;
	@Inject
	AiClassService aiClassService;
	@Inject
	FileUploadService fileUploadService;
	@Inject
	AiParentService aiParentService; 

	/**
	 * 获取班级成员
	 */
	public void getMembers() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		// 获取参数
		int login_class_id = this.getAttrForInt("login_class_id");		

		// 获取分页
		Object teacherData = aiTeacherService.getTeachers(login_class_id);
		jr.setDicKV("list_teachers", teacherData);
		Object studentsData = aiStudentService.getStudents(login_class_id);
		jr.setDicKV("list_students", studentsData);
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void getStudent() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		int login_org_id = this.getAttrForInt("login_org_id");
		
		// 获取参数
		int student_id = getInt("stuid", 0);	

		Object obj = aiStudentService.getOne(student_id);
		jr.setDicKV("studata", obj);
		obj = aiClassService.getAll(login_org_id, 1);
		if (obj != null) { jr.setDicKV("listclass", obj); }
		
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
		
	}
	public void getParents() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		// 获取参数
		int student_id = getInt("stuid", 0);	

		Object obj = aiParentService.getParents(student_id);
		jr.setDicKV("listparents", obj);
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
		
	}
	

	public void upload_stuhead() {
		
		this.setAttr("filemodel", "base64");
		
		this.setAttr("zoom_type", "wh");
		this.setAttr("zoom_w", 100);
		this.setAttr("zoom_h", 100);
		
		JsonResult jr = fileUploadService.uploadFile(this);
		if (jr.getCode() == 0) {
			AiStudent info = new AiStudent();
			info.setStudentId(getInt("stuid"));
			info.setStudentHeadpic(jr.getDicKey("fileurl"));
			info.setUpdateTime(DateUtil.getTimestamp());
			info.removeNullValueAttrs();
			boolean bf = info.update();
			if (bf) {
				
			}else {
				String msg = jr.getMsg();
				jr.setEnum(JsonCodeEnum.Faild);
				jr.appendMsg(msg);
			}
		}
		renderJson(jr.Dic);
	}
	
	
	public void update_info() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		AiStudent info = getModel(AiStudent.class, "", true);
		info.setUpdateTime(DateUtil.getTimestamp());
		boolean bf = info.update();
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		
		renderJson(jr.Dic);
	}
	
	public void update_rollcall() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		AiStudent info = getModel(AiStudent.class, "", true);
		info.setUpdateTime(DateUtil.getTimestamp());
		boolean bf = info.update();
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		
		renderJson(jr.Dic);
		
	}

	public void delStudent() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		int student_id = getInt("stuid", 0);
		
		AiStudent info = new AiStudent();
		info.setStudentId(student_id);
		info.setUpdateTime(DateUtil.getTimestamp());
		info.setStudentStateid(-1);
		boolean bf = info.update();
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		
		renderJson(jr.Dic);
	}
	
	public void delParent() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		int parent_id = getInt("pid", 0);
		

		boolean bf = aiParentService.del(parent_id);
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		
		renderJson(jr.Dic);
	}
	
}
